home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.2 / Libraries / DOS / dos_calls.c next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  19.2 KB  |  1,133 lines

  1. /*
  2.  * Copyright (C) 1990 Commodore-Amiga, Inc.
  3.  * All rights reserved
  4.  */
  5.  
  6. /* dos_calls.c
  7.  * Test code for many DOS calls
  8.  * Compiled with Lattice C 5.05: lc -cfist -v -L doscalls.c 
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <dos/dosextens.h>
  14. #include <dos/record.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/dos_protos.h>
  18.  
  19. /* normally pragmas would be included here */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. #define SAME 0
  26.  
  27. /* so the compiler doesn't get upset */
  28. int main (int, char **);
  29.  
  30. void help(int *done);
  31. void quit(int *done);
  32. void source(int *done);
  33. void showbuffer(int *done);
  34. void open(int *done);
  35. void close(int *done);
  36. void showfiles(int *done);
  37. void lock(int *done);
  38. void unlock(int *done);
  39. void duplock(int *done);
  40. void showlocks(int *done);
  41. void myread(int *done);
  42. void mywrite(int *done);
  43. void lockrecord(int *done);
  44. void unlockrecord(int *done);
  45. void myfgets(int *done);
  46. void myfputs(int *done);
  47. void myfgetc(int *done);
  48. void myfputc(int *done);
  49. void myseek(int *done);
  50. void myflush(int *done);
  51. void dumpfh(int *done);
  52. void dumplock(int *done);
  53. void samelock(int*done);
  54. void execute(int *done);
  55. void deletefile(int *done);
  56. void splitname(int *done);
  57. void findvar(int *done);
  58. void duplockfromfh(int *done);
  59. void namefromlock(int *done);
  60. void namefromfh(int *done);
  61. void parentdir(int *done);
  62. void parentoffh(int *done);
  63. void examine(int *done);
  64. void exnext(int *done);
  65.  
  66. /* more commands can be _easily_ added */
  67.  
  68. struct commands {
  69.     char *name;
  70.     char *helpstr;
  71.     void (*rtn)(int *done);
  72. } comm[] = {
  73.     "?"," - list all commands",help,
  74.     "quit"," - exit this program",quit,
  75.     "source","<file> - read commands from file",source,
  76.     "showbuffer","[hex] [offset [length]] - show part of buffer",
  77.         showbuffer,
  78.     "open","<filename> <mode> - open a file",open,
  79.     "close","<file> - close a file",close,
  80.     "showfiles"," - show all open files",showfiles,
  81.     "lock","<name> <mode> - lock an object",lock,
  82.     "unlock","<lock> - unlock a lock",unlock,
  83.     "duplock","<lock> - duplicate a lock",duplock,
  84.     "ParentDir","<lock> - get a lock on the parent of a lock",parentdir,
  85.     "Examine","<lock> <offset> - examine a lock",examine,
  86.     "ExNext","<lock> <offset> - examine next file/dir",exnext,
  87.     "ParentOfFH","<file> - returns lock on parent dir of object",parentoffh,
  88.     "DupLockFromFH","<number> - get a lock on open file",duplockfromfh,
  89.     "NameFromLock","<lock> <offset> <length> - get name of object",
  90.         namefromlock,
  91.     "NameFromFH","<file> <offset> <length> - get name of object",
  92.         namefromfh,
  93.     "showlocks"," - show all locks",showlocks,
  94.     "read","<file> <offset> <length> - read bytes from file",myread,
  95.     "write","<file> <offset> <length> - write bytes from file",mywrite,
  96.     "seek","<file> <offset> <mode> - seek to position in file",myseek,
  97.     "lockrecord","<file> <offset> <length> <mode> <timeout>- lock a part of a file",
  98.         lockrecord,
  99.     "unlockrecord","<file> <offset> <length> - unlock a part of a file",
  100.         unlockrecord,
  101.     "FGets","<file> <offset> <length> - buffered read to len or \\n",
  102.         myfgets,
  103.     "FPuts","<file> <offset>|<string> - buffered write of string",myfputs,
  104.     "FGetC","<file> <offset> - buffered character read",myfgetc,
  105.     "FPutC","<file> <offset>|<'char'> - buffered character write",myfputc,
  106.     "Flush","<file> - flush IO buffers for file",myflush,
  107.     "SameLock","<lock> <lock> - determine if two locks are on same object",
  108.         samelock,
  109.     "Execute","<command> <input file> <output file>",execute,
  110.     "DeleteFile","<filename> - delete a file or directory",deletefile,
  111.     "SplitName","<string> <'char'> <offset> <old pos> <size>",splitname,
  112.     "FindVar","<name> <type> - find a local variable",findvar,
  113.     "DumpFH","<file> - show filehandle",dumpfh,
  114.     "DumpLock","<lock> - show filelock",dumplock,
  115. };
  116.  
  117. extern struct DosLibrary *DOSBase;
  118.  
  119. /* filehandle for input */
  120. FILE *fp = stdin;
  121.  
  122. int
  123. main (argc,argv)
  124.     int argc;
  125.     char **argv;
  126. {
  127.     char buff[256];
  128.     int done = FALSE,i,len;
  129.     char *command;
  130.  
  131.     if (argc != 1)
  132.     {
  133.         printf("usage: %s\n",argv[0]);
  134.         exit(10);
  135.     }
  136.  
  137.     if (DOSBase->dl_lib.lib_Version < 36 ||
  138.         DOSBase->dl_lib.lib_Revision < 30)
  139.     {
  140.         printf("You need a doslibrary V36R30 or better!\n");
  141.         exit(20);
  142.     }
  143.  
  144.     printf("%s 1.0 - use ? to get a list of commands\n\n",argv[0]);
  145.     printf("\tWarning: there is little error checking in this program\n");
  146.     while (!done)
  147.     {
  148.         if (fp == stdin)
  149.         {
  150.             printf("%s> ",argv[0]);
  151.             fflush(stdout);
  152.         }
  153.         if (fgets(buff,sizeof(buff),fp) == NULL)
  154.         {
  155.             if (fp == stdin)
  156.                 break;            /* eof */
  157.             else {
  158.                 fp = stdin;        /* end of 'source' */
  159.                 continue;
  160.             }
  161.         }
  162.  
  163.         len = strlen(buff);        /* kill \n */
  164.         if (len && buff[len-1] == '\n')
  165.             buff[len-1] = '\0';
  166.  
  167.         command = strtok(buff," ");
  168.         if (!command || !*command)        /* no command */
  169.             continue;
  170.  
  171.         for (i = 0; i < sizeof(comm)/sizeof(comm[0]); i++)
  172.         {
  173.             if (stricmp(command,comm[i].name) == SAME)
  174.             {
  175.                 /* found it - execute */
  176.  
  177.                 (*(comm[i].rtn))(&done);
  178.                 break;
  179.             }
  180.         }
  181.         if (i >= sizeof(comm)/sizeof(comm[0]))    /* failure */
  182.             printf("Error: command %s not known!\n",command);
  183.     }
  184.  
  185.     return 0;
  186. }
  187.  
  188. void
  189. help (int *done)
  190. {
  191.     int i;
  192.  
  193.     for (i = 0; i < sizeof(comm)/sizeof(comm[0]); i++)
  194.     {
  195.         printf(" %s %s\n",comm[i].name,comm[i].helpstr);
  196.     }
  197.     printf("\n <file> or <lock> refers to an index into the arrays\n");
  198.     printf(" <offset> refers to an offset into the buffer\n");
  199. }
  200.  
  201. void
  202. quit (int *done)
  203. {
  204.     *done = TRUE;
  205. }
  206.  
  207. void
  208. source (int *done)
  209. {
  210.     char *arg;
  211.     FILE *myfp;
  212.  
  213.     arg = strtok(NULL," ");
  214.     if (!arg)
  215.     {
  216.         printf("error: use source <filename>\n");
  217.         return;
  218.     }
  219.  
  220.     myfp = fopen(arg,"r");
  221.     if (!myfp)
  222.     {
  223.         printf("error: Can't open %s!\n",arg);
  224.         return;
  225.     }
  226.  
  227.     fp = myfp;
  228. }
  229.  
  230. unsigned char buffer[1024];
  231.  
  232. void
  233. showbuffer (int *done)
  234. {
  235.     char *arg;
  236.     int i;
  237.     int hex = FALSE;
  238.     long offset = 0;
  239.     long len    = 80;
  240.  
  241.     arg = strtok(NULL," ");
  242.     if (arg)
  243.     {
  244.         if (stricmp(arg,"hex") == SAME)
  245.         {
  246.             hex = TRUE;
  247.             arg = strtok(NULL," ");
  248.         }
  249.     }
  250.  
  251.     /* optional offset */
  252.     if (arg)
  253.     {
  254.         if (strlen(arg) != stcd_l(arg,&offset))
  255.         {
  256. err:            printf("error: bad number (%s)\n",arg);
  257.             printf("error: use showbuffer [hex] [offset [length]]\n");
  258.             return;
  259.         }
  260.  
  261.         arg = strtok(NULL," ");
  262.         if (arg)
  263.         {
  264.             if (strlen(arg) != stcd_l(arg,&len))
  265.             {
  266.                 goto err;
  267.             }
  268.         }
  269.     }
  270.  
  271.     for (i = 0; i < len; i++)
  272.     {
  273.         if (!hex)
  274.             putchar(buffer[offset+i]);
  275.         else {
  276.             if (i % 20 == 0)
  277.             {
  278.                 printf("\n%04ld: ",i+offset);
  279.             }
  280.             printf("%02x ",buffer[offset+i]);
  281.         }
  282.     }
  283.     putchar('\n');
  284. }
  285.  
  286. /* end standard code */
  287.  
  288. /* data */
  289.  
  290. BPTR files[10];
  291. BPTR locks[10];
  292. char *lastarg;        /* for getnum failures */
  293.  
  294. int
  295. getnum (long *num)
  296. {
  297.     lastarg = strtok(NULL," ");
  298.     if (!lastarg)
  299.         return FALSE;
  300.  
  301.     if (strlen(lastarg) != stcd_l(lastarg,num))
  302.     {
  303.         printf("error: bad number (%s)\n",lastarg);
  304.         return FALSE;
  305.     }
  306.  
  307.     return TRUE;
  308. }
  309.  
  310. int
  311. get2nums (long *num1, long *num2)
  312. {
  313.     if (!getnum(num1))
  314.         return FALSE;
  315.  
  316.     return getnum(num2);
  317. }
  318.  
  319. int
  320. get3nums (long *num1, long *num2, long *num3)
  321. {
  322.     if (!getnum(num1) || !getnum(num2))
  323.         return FALSE;
  324.  
  325.     return getnum(num3);
  326. }
  327.  
  328. /* open file */
  329. void
  330. open (int *done)
  331. {
  332.     char *name,*modestr;
  333.     LONG mode;
  334.     BPTR fh;
  335.     int i;
  336.  
  337.     name = strtok(NULL," ");
  338.     if (!name)
  339.     {
  340. err:        printf("error: use open <filename> <mode>\n");
  341.         return;
  342.     }
  343.  
  344.     modestr = strtok(NULL," ");
  345.     if (!modestr)
  346.         goto err;
  347.  
  348.     if (stricmp(modestr,"mode_oldfile") == SAME)
  349.         mode = MODE_OLDFILE;
  350.     else if (stricmp(modestr,"mode_newfile") == SAME)
  351.         mode = MODE_NEWFILE;
  352.     else
  353.         if (strlen(modestr) != stcd_l(modestr,&mode))
  354.         {
  355.             printf("error: bad number (%s)\n",modestr);
  356.             return;
  357.         }
  358.  
  359.     fh = Open(name,mode);
  360.     if (!fh)
  361.     {
  362.         printf("error: Can't open %s, ioerr = %ld\n",name,IoErr());
  363.         return;
  364.     }
  365.  
  366.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  367.     {
  368.         if (files[i] == NULL)
  369.         {
  370.             files[i] = fh;
  371.             printf("Opened as file %d\n",i);
  372.             return;
  373.         }
  374.     }
  375.  
  376.     printf("no space for more files!\n");
  377.     Close(fh);
  378. }
  379.  
  380. void
  381. close (int *done)
  382. {
  383.     int num;
  384.  
  385.     if (!getnum(&num))
  386.     {
  387.         printf("error: use close <filenumber>\n");
  388.         return;
  389.     }
  390.  
  391.     Close(files[num]);
  392.     files[num] = NULL;
  393. }
  394.  
  395. void
  396. showfiles (int *done)
  397. {
  398.     int i;
  399.  
  400.     printf("\tNumber\t   FH\n");
  401.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  402.     {
  403.         if (files[i])
  404.             printf("\t%d\t- 0x%lx\n",i,files[i]);
  405.     }
  406. }
  407.  
  408. /* open file */
  409. void
  410. lock (int *done)
  411. {
  412.     char *name,*modestr;
  413.     LONG mode;
  414.     BPTR lock;
  415.     int i;
  416.  
  417.     name = strtok(NULL," ");
  418.     if (!name)
  419.     {
  420. err:        printf("error: use lock <name> <mode>\n");
  421.         return;
  422.     }
  423.  
  424.     modestr = strtok(NULL," ");
  425.     if (!modestr)
  426.         goto err;
  427.  
  428.     if (stricmp(modestr,"shared") == SAME)
  429.         mode = SHARED_LOCK;
  430.     else if (stricmp(modestr,"exclusive") == SAME)
  431.         mode = EXCLUSIVE_LOCK;
  432.     else
  433.         if (strlen(modestr) != stcd_l(modestr,&mode))
  434.         {
  435.             printf("error: bad number (%s)\n",modestr);
  436.             return;
  437.         }
  438.  
  439.     lock = Lock(name,mode);
  440.     if (!lock)
  441.     {
  442.         printf("error: Can't lock %s, ioerr = %ld\n",name,IoErr());
  443.         return;
  444.     }
  445.  
  446.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  447.     {
  448.         if (locks[i] == NULL)
  449.         {
  450.             locks[i] = lock;
  451.             printf("Locked as lock %d\n",i);
  452.             return;
  453.         }
  454.     }
  455.  
  456.     printf("no space for more locks!\n");
  457.     UnLock(lock);
  458. }
  459.  
  460. void
  461. unlock (int *done)
  462. {
  463.     int num;
  464.  
  465.     if (!getnum(&num))
  466.     {
  467.         printf("error: use unlock <locknumber>\n");
  468.         return;
  469.     }
  470.  
  471.     UnLock(locks[num]);
  472.     locks[num] = NULL;
  473. }
  474.  
  475. void
  476. duplock (int *done)
  477. {
  478.     int num,i;
  479.     BPTR lock;
  480.  
  481.     if (!getnum(&num))
  482.     {
  483.         printf("error: use duplock <locknumber>\n");
  484.         return;
  485.     }
  486.  
  487.     lock = DupLock(locks[num]);
  488.     if (!lock)
  489.     {
  490.         printf("error: Can't duplock 0x%lx, ioerr = %ld\n",
  491.             locks[num],IoErr());
  492.         printf(" (note: DupLock(0) == 0)\n");
  493.         return;
  494.     }
  495.  
  496.  
  497.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  498.     {
  499.         if (locks[i] == NULL)
  500.         {
  501.             locks[i] = lock;
  502.             printf("Locked as lock %d\n",i);
  503.             return;
  504.         }
  505.     }
  506.  
  507.     printf("no space for more locks!\n");
  508.     UnLock(lock);
  509. }
  510.  
  511. void
  512. parentdir (int *done)
  513. {
  514.     int num,i;
  515.     BPTR lock;
  516.  
  517.     if (!getnum(&num))
  518.     {
  519.         printf("error: use ParentDir <locknumber>\n");
  520.         return;
  521.     }
  522.  
  523.     lock = ParentDir(locks[num]);
  524.     if (!lock)
  525.     {
  526.         printf("error: Can't get parent of 0x%lx, ioerr = %ld\n",
  527.             locks[num],IoErr());
  528.         return;
  529.     }
  530.  
  531.  
  532.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  533.     {
  534.         if (locks[i] == NULL)
  535.         {
  536.             locks[i] = lock;
  537.             printf("Locked as lock %d\n",i);
  538.             return;
  539.         }
  540.     }
  541.  
  542.     printf("no space for more locks!\n");
  543.     UnLock(lock);
  544. }
  545.  
  546. void
  547. duplockfromfh (int *done)
  548. {
  549.     int num,i;
  550.     BPTR lock;
  551.  
  552.     if (!getnum(&num))
  553.     {
  554.         printf("error: use DupLockFromFH <filenumber>\n");
  555.         return;
  556.     }
  557.  
  558.     lock = DupLockFromFH(files[num]);
  559.     if (!lock)
  560.     {
  561.         printf("error: Can't duplockfromfh 0x%lx, ioerr = %ld\n",
  562.             files[num],IoErr());
  563.         return;
  564.     }
  565.  
  566.  
  567.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  568.     {
  569.         if (locks[i] == NULL)
  570.         {
  571.             locks[i] = lock;
  572.             printf("Locked as lock %d\n",i);
  573.             return;
  574.         }
  575.     }
  576.  
  577.     printf("no space for more locks!\n");
  578.     UnLock(lock);
  579. }
  580.  
  581. void
  582. parentoffh (int *done)
  583. {
  584.     int num,i;
  585.     BPTR lock;
  586.  
  587.     if (!getnum(&num))
  588.     {
  589.         printf("error: use ParentOfFH <filenumber>\n");
  590.         return;
  591.     }
  592.  
  593.     lock = ParentOfFH(files[num]);
  594.     if (!lock)
  595.     {
  596.         printf("error: Can't get parent of 0x%lx, ioerr = %ld\n",
  597.             files[num],IoErr());
  598.         return;
  599.     }
  600.  
  601.  
  602.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  603.     {
  604.         if (locks[i] == NULL)
  605.         {
  606.             locks[i] = lock;
  607.             printf("Locked as lock %d\n",i);
  608.             return;
  609.         }
  610.     }
  611.  
  612.     printf("no space for more locks!\n");
  613.     UnLock(lock);
  614. }
  615.  
  616. void
  617. examine (int *done)
  618. {
  619.     struct FileInfoBlock *fib;
  620.     LONG num,offset,rc;
  621.  
  622.     if (!getnum(&num))
  623.     {
  624. err:        printf("error: use Examine <lock> <offset>\n");
  625.         return;
  626.     }
  627.  
  628.     if (!getnum(&offset))
  629.         goto err;
  630.  
  631.     fib = (void *) &buffer[offset];
  632.  
  633.     rc = Examine(locks[num],fib);
  634.     printf("Examine returned %ld, ioerr = %ld\n",rc,IoErr());
  635. }
  636.  
  637. void
  638. exnext (int *done)
  639. {
  640.     struct FileInfoBlock *fib;
  641.     LONG num,offset,rc;
  642.  
  643.     if (!getnum(&num))
  644.     {
  645. err:        printf("error: use ExNext <lock> <offset>\n");
  646.         return;
  647.     }
  648.  
  649.     if (!getnum(&offset))
  650.         goto err;
  651.  
  652.     fib = (void *) &buffer[offset];
  653.  
  654.     rc = ExNext(locks[num],fib);
  655.     printf("ExNext returned %ld, ioerr = %ld\n",rc,IoErr());
  656. }
  657.  
  658. void
  659. showlocks (int *done)
  660. {
  661.     int i;
  662.  
  663.     printf("\tNumber\t   Lock\n");
  664.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  665.     {
  666.         if (locks[i])
  667.             printf("\t%d\t- 0x%lx\n",i,locks[i]);
  668.     }
  669. }
  670.  
  671. void
  672. myread (int *done)
  673. {
  674.     long offset,length,file,len;
  675.  
  676.     if (!get3nums(&file,&offset,&length))
  677.     {
  678.         printf("error: use read <filenumber> <offset> <length>\n");
  679.         return;
  680.     }
  681.  
  682.     len = Read(files[file],&(buffer[offset]),length);
  683.     if (len < 0)
  684.     {
  685.         printf("error on read - %ld\n",IoErr());
  686.         return;
  687.     }
  688.  
  689.     printf("Read %ld characters\n",len);
  690. }
  691.  
  692. void
  693. mywrite (int *done)
  694. {
  695.     long offset,length,file,len;
  696.  
  697.     if (!get3nums(&file,&offset,&length))
  698.     {
  699.         printf("error: use write <filenumber> <offset> <length>\n");
  700.         return;
  701.     }
  702.  
  703.     len = Write(files[file],&(buffer[offset]),length);
  704.     if (len < 0)
  705.     {
  706.         printf("error on write - %ld\n",IoErr());
  707.         return;
  708.     }
  709.  
  710.     printf("Wrote %ld characters\n",len);
  711. }
  712.  
  713. void
  714. myseek (int *done)
  715. {
  716.     long offset,mode,file,oldpos;
  717.  
  718.     if (!get3nums(&file,&offset,&mode))
  719.     {
  720.         printf("error: use seek <filenumber> <offset> <mode>\n");
  721.         return;
  722.     }
  723.  
  724.     oldpos = Seek(files[file],offset,mode);
  725.     if (oldpos < 0)
  726.     {
  727.         printf("error on seek - %ld\n",IoErr());
  728.         return;
  729.     }
  730.  
  731.     printf("old position was %ld\n",oldpos);
  732. }
  733.  
  734. void
  735. lockrecord (int *done)
  736. {
  737.     long offset,length,file,res,mode,timeout = 50;
  738.  
  739.     if (!get3nums(&file,&offset,&length) ||
  740.         !get2nums(&mode,&timeout))
  741.     {
  742.      printf("error: use lockrecord <filenumber> <offset> <length> <mode>\n");
  743.      return;
  744.     }
  745.  
  746.     res = LockRecord(files[file],offset,length,mode,timeout);
  747.     if (res == 0)
  748.     {
  749.         printf("error on lock - %ld\n",IoErr());
  750.         return;
  751.     }
  752.  
  753.     printf("Locked (%ld,%ld)\n",res,IoErr());
  754. }
  755.  
  756. void
  757. unlockrecord (int *done)
  758. {
  759.     long offset,length,file,res;
  760.  
  761.     if (!get3nums(&file,&offset,&length))
  762.     {
  763.      printf("error: use unlockrecord <filenumber> <offset> <length>\n");
  764.      return;
  765.     }
  766.  
  767.     res = UnLockRecord(files[file],offset,length);
  768.     if (res == 0)
  769.     {
  770.         printf("error on unlock - %ld\n",IoErr());
  771.         return;
  772.     }
  773.  
  774.     printf("Unlocked\n");
  775. }
  776.  
  777. void
  778. myfgets (int *done)
  779. {
  780.     long offset,length,file;
  781.     char *p;
  782.  
  783.     if (!get3nums(&file,&offset,&length))
  784.     {
  785.         printf("error: use fgets <filenumber> <offset> <length>\n");
  786.         return;
  787.     }
  788.  
  789.     p = (char *) FGets(files[file],&(buffer[offset]),length);
  790.     if (p == 0)
  791.     {
  792.         printf("eof or error on FGets - %ld\n",IoErr());
  793.         return;
  794.     }
  795.  
  796.     printf("Read %ld characters\n",strlen(p));
  797. }
  798.  
  799. void
  800. myfputs (int *done)
  801. {
  802.     long offset,file;
  803.     unsigned char *p;
  804.  
  805.     if (!getnum(&file))
  806.     {
  807. err:        printf("error: use fputs <filenumber> <offset>|<string>\n");
  808.         return;
  809.     }
  810.     if (!(p = strtok(NULL," ")))
  811.         goto err;
  812.  
  813.     if (strlen(p) == stcd_l(p,&offset))
  814.     {
  815.         p = &(buffer[offset]);
  816.     }
  817.  
  818.     if (FPuts(files[file],p))
  819.     {
  820.         printf("eof or error on FPuts - %ld\n",IoErr());
  821.         return;
  822.     }
  823.  
  824.     printf("Wrote %ld characters\n",strlen(p));
  825. }
  826.  
  827. void
  828. myfgetc (int *done)
  829. {
  830.     long offset,file;
  831.     long p;
  832.  
  833.     if (!get2nums(&file,&offset))
  834.     {
  835.         printf("error: use fgetc <filenumber> <offset>\n");
  836.         return;
  837.     }
  838.  
  839.     p = FGetC(files[file]);
  840.     if (p == -1)
  841.     {
  842.         printf("eof on FGetC - %ld\n",IoErr());
  843.         return;
  844.     }
  845.  
  846.     buffer[offset] = p;
  847.  
  848.     printf("Read 0x%lx\n",p);
  849. }
  850.  
  851. void
  852. myfputc (int *done)
  853. {
  854.     long offset,file;
  855.     unsigned char *p;
  856.  
  857.     if (!getnum(&file))
  858.     {
  859. err:        printf("error: use fputc <filenumber> <offset>|<'char'>\n");
  860.         return;
  861.     }
  862.     if (!(p = strtok(NULL," ")))
  863.         goto err;
  864.  
  865.     if (strlen(p) == stcd_l(p,&offset))
  866.     {
  867.         p = &(buffer[offset]);
  868.     }
  869.  
  870.     FPutC(files[file],*p);
  871.  
  872.     printf("Wrote 0x%lx\n",*p);
  873. }
  874.  
  875. void
  876. myflush (int *done)
  877. {
  878.     long file;
  879.  
  880.     if (!getnum(&file))
  881.     {
  882.         printf("error: use flush <filenumber>\n");
  883.         return;
  884.     }
  885.  
  886.     Flush(files[file]);
  887.  
  888.     printf("flushed\n");
  889. }
  890.  
  891. void
  892. dumpfh (int *done)
  893. {
  894.     long file;
  895.     struct FileHandle *fh;
  896.  
  897.     if (!getnum(&file))
  898.     {
  899.         printf("error: use dumpfh <filenumber>\n");
  900.         return;
  901.     }
  902.  
  903.     fh = (void *) BADDR(files[file]);
  904.     printf("link = 0x%lx, port = 0x%lx, type = 0x%lx\n",
  905.         fh->fh_Link,fh->fh_Port,fh->fh_Type);
  906.     printf("buf = 0x%lx, pos = %ld, end = %ld\n",
  907.         fh->fh_Buf,fh->fh_Pos,fh->fh_End);
  908.     printf("func1 = 0x%lx, func2 = 0x%lx, func3 = 0x%lx\n",
  909.         fh->fh_Func1,fh->fh_Func2,fh->fh_Func3);
  910.     printf("fh_Arg1 = 0x%lx, fh-Arg2 = 0x%lx\n",
  911.         fh->fh_Arg1,fh->fh_Arg2);
  912. }
  913.  
  914. void
  915. dumplock (int *done)
  916. {
  917.     long num;
  918.     struct FileLock *lock;
  919.  
  920.     if (!getnum(&num))
  921.     {
  922.         printf("error: use dumplock <locknumber>\n");
  923.         return;
  924.     }
  925.  
  926.     lock = (void *) BADDR(locks[num]);
  927.     if (!lock)
  928.     {
  929.         printf("lock on boot fs root (NULL)\n");
  930.         return;
  931.     }
  932.     printf("link = 0x%lx, key = 0x%lx, access = 0x%lx\n",
  933.         lock->fl_Link,lock->fl_Key,lock->fl_Access);
  934.     printf("port = 0x%lx, volume = %ld (%s)\n",
  935.         lock->fl_Task,lock->fl_Volume, (lock->fl_Volume ?
  936.             ((char *) BADDR(((struct DosList *)
  937.                     BADDR(lock->fl_Volume))->dol_Name))+1 :
  938.             ""));
  939. }
  940.  
  941. void
  942. samelock (int *done)
  943. {
  944.     LONG rc;
  945.     BPTR lock1,lock2;
  946.  
  947.     if (!get2nums(&lock1,&lock2))
  948.     {
  949.         printf("error: use samelock <lock1> <lock2>\n");
  950.         return;
  951.     }
  952.  
  953.     rc = SameLock(locks[lock1],locks[lock2]);
  954.  
  955.     printf("SameLock returned %ld\n",rc);
  956. }
  957.  
  958. void
  959. execute (int *done)
  960. {
  961.     char *command;
  962.     long input,output;
  963.     long rc;
  964.     char buf[256];
  965.  
  966.     command = strtok(NULL," ");
  967.     if (!command)
  968.     {
  969. err:        printf("error: use execute <command> <input fh> <output fh>\n");
  970.         return;
  971.     }
  972.     if (*command == '"')
  973.     {
  974.         /* strip quotes */
  975.         command++;
  976.         if (command[strlen(command)-1] == '"')
  977.         {
  978.             command[strlen(command)-1] = 0;
  979.         } else {
  980.             strcpy(buf,command);
  981.             do {
  982.                 command = strtok(NULL," ");
  983.                 if (!command)
  984.                     goto err;
  985.                 strcat(buf,command);
  986.             } while (command[strlen(command)-1] != '"');
  987.             buf[strlen(buf)-1] = 0;
  988.             command = buf;
  989.         }
  990.     }
  991.  
  992.     if (!get2nums(&input,&output))
  993.         goto err;
  994.  
  995.     if (input == -1)
  996.         input = NULL;
  997.     else
  998.         input = files[input];
  999.  
  1000.     if (output == -1)
  1001.         output = NULL;
  1002.     else
  1003.         output = files[output];
  1004.  
  1005.     printf("Execute(\"%s\",0x%lx,0x%lx)\n",command,input,output);
  1006.  
  1007.     rc = Execute(command,input,output);
  1008.  
  1009.     printf("Execute returned %ld (ioerr = %ld)\n",rc,IoErr());
  1010. }
  1011.  
  1012. void
  1013. deletefile (int *done)
  1014. {
  1015.     LONG rc;
  1016.  
  1017.     rc = DeleteFile(strtok(NULL," "));
  1018.  
  1019.     printf("DeleteFile returned %ld, ioerr = %ld\n",rc,IoErr());
  1020. }
  1021.  
  1022. void
  1023. splitname (int *done)
  1024. {
  1025.     char *string,*separator;
  1026.     unsigned char *buf;
  1027.     LONG size,pos,offset;
  1028.  
  1029.     string = strtok(NULL," ");
  1030.     if (!string)
  1031.     {
  1032. err:    printf("usage: SplitName <string> <'char'> <offset> <old pos> <size>\n");
  1033.     return;
  1034.     }
  1035.  
  1036.     separator = strtok(NULL," ");
  1037.     if (!separator)
  1038.         goto err;
  1039.  
  1040.     if (!getnum(&offset))
  1041.         goto err;
  1042.  
  1043.     buf = &(buffer[offset]);
  1044.  
  1045.     if (!getnum(&pos))
  1046.         goto err;
  1047.  
  1048.     if (!getnum(&size))
  1049.         goto err;
  1050.  
  1051.     printf("SplitName(%s,%lc,0x%lx,%ld,%ld)\n",
  1052.         string,*separator,buf,pos,size);
  1053.     pos = SplitName(string,*separator,buf,(UWORD) pos,size);
  1054.     printf("  returned %ld (ioerr = %ld)\n",pos,IoErr());
  1055.  
  1056. }
  1057.  
  1058. void
  1059. findvar (int *done)
  1060. {
  1061.     char *name;
  1062.     LONG type;
  1063.     struct LocalVar *lv;
  1064.  
  1065.     name = strtok(NULL," ");
  1066.     if (!name)
  1067.     {
  1068. err:        printf("usage: FindVar <name> <type>\n");
  1069.         return;
  1070.     }
  1071.  
  1072.     if (!getnum(&type))
  1073.         goto err;
  1074.  
  1075.     lv = FindVar(name,type);
  1076.     printf("FindVar(%s,%ld) returned 0x%lx\n",name,type,lv);
  1077. }
  1078.  
  1079. void
  1080. namefromlock (int *done)
  1081. {
  1082.     unsigned char *buf;
  1083.     LONG size,num,offset,rc;
  1084.  
  1085.     if (!getnum(&num))
  1086.     {
  1087. err:    printf("usage: NameFromLock <lock> <offset> <length>\n");
  1088.     return;
  1089.     }
  1090.  
  1091.     if (!getnum(&offset))
  1092.         goto err;
  1093.  
  1094.     buf = &(buffer[offset]);
  1095.  
  1096.     if (!getnum(&size))
  1097.         goto err;
  1098.  
  1099.     rc = NameFromLock(locks[num],buf,size);
  1100.     printf("NameFromLock returned %ld (ioerr = %ld)\n",rc,IoErr());
  1101.  
  1102.     if (rc)
  1103.         printf("\t\"%s\"\n",buf);
  1104. }
  1105.  
  1106. void
  1107. namefromfh (int *done)
  1108. {
  1109.     unsigned char *buf;
  1110.     LONG size,num,offset,rc;
  1111.  
  1112.     if (!getnum(&num))
  1113.     {
  1114. err:    printf("usage: NameFromFH <file> <offset> <length>\n");
  1115.     return;
  1116.     }
  1117.  
  1118.     if (!getnum(&offset))
  1119.         goto err;
  1120.  
  1121.     buf = &(buffer[offset]);
  1122.  
  1123.     if (!getnum(&size))
  1124.         goto err;
  1125.  
  1126.     rc = NameFromFH(files[num],buf,size);
  1127.     printf("NameFromFH returned %ld (ioerr = %ld)\n",rc,IoErr());
  1128.  
  1129.     if (rc)
  1130.         printf("\t\"%s\"\n",buf);
  1131. }
  1132.  
  1133.